<?php
namespace Com\Pay;
// 好付支付
class HaofuPay{

    private $config = array();

    public function __construct($mchData){
        $this->config = $mchData;
    }

    /*生成支付连接*/
    public function getUrl($order_info){
    // https://api.zhifujiekou.com/api/mpgateway
        $merchantCode = $this->config['mch_id'];
        $notifyUrl = S_URL . '/Api/NotifyCallback/HaofuPayNotify';
        $order_sn = $order_info['ordernumber'];
        $price = $order_info['paymoney'] * 100;
        $Senddata = array(
            'txnamt' => $price,
            "merid"  =>  $merchantCode,
            'orderid' => $order_sn,
            "backurl"  =>  $notifyUrl,
            "fronturl"  =>  $order_info['return_url'],
        );
//        dump($Senddata);die;
        $bw = base64_encode(json_encode($Senddata));
        $Sign = $this->setSign($bw , false);
        $Send_url = 'https://api.zhifujiekou.com/apis/wxjspaygateway?req=' . urlencode($bw) . '&sign=' . $Sign;
//        exit($Send_url);
        return $Send_url;
    }

    private function setSign($data,$isNotify){
        $sign = md5($data . $this->config['mch_key']);
        return $sign;
    }

    //回调方法，把返回数据处理成统一规定返回
    public function response(){
        $data = parseData();
        $data1 = json_decode(base64_decode($data['resp']) , true);
        $Sign = $data['sign'];
        // 进行签名验证
        $MySign =  $this->setSign($data['resp'],true);
        if($MySign == $Sign){
            // 验签成功
            if($data1['resultcode'] == '1002'){
                return true;
            }else{
                return false;
            }
        }else{
            // 失败
            return false;
        }
    }

}
